from django import forms

from goods.models import GoodsCategory


class GoodsVerify(forms.Form):
    name = forms.CharField(required=True,error_messages={
        'required':'商品名称必填'
    })
    goods_sn = forms.CharField(required=True,error_messages={
        'required':'商品货号必填'
    })
    category = forms.CharField(required=True,error_messages={
        'required':'种类必选'
    })
    goods_nums = forms.CharField(required=True,error_messages={
        'required':'商品库存必填'
    })
    market_price = forms.CharField(required=True,error_messages={
        'required':'市场价格必填'
    })
    shop_price = forms.CharField(required=True,error_messages={
        'required':'本店价格必填'
    })
    goods_brief = forms.CharField(required=True,error_messages={
        'required':'描述必填'
    })
    goods_front_image = forms.ImageField(required=False,error_messages={
        'required':'图片需上传'
    })

    def clean_category(self):
        # 校验哪个字段，就把字段放在clean后面
        id = self.cleaned_data.get('category')
        category = GoodsCategory.objects.filter(pk=id).first()
        return category